import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';
import {StoragePage} from "../storage/storage";
import {BarcodeScanner} from "@ionic-native/barcode-scanner";
import {services} from "../../../providers/service";
import {AppPopup} from "../../../util/popup";
import {StorageService} from "../../../providers/storage-service";

@Component({
    selector: 'cjDelStatus',
    templateUrl: 'cjDelStatus.html',
})
export class CjDelStatusPage {
    showAlert = true;
    status = true;
    data;
    isSuccess: boolean = true;  //默认采集成功状态

    constructor(public navCtrl: NavController,
                public navParams: NavParams,
                private service: services,
                public barcodeScanner: BarcodeScanner,
                private popup: AppPopup,
                private storageServ: StorageService,) {
        this.data = navParams.get("data");
        if (this.data.resultStatus == "success") {
            this.isSuccess = true;      //采集成功
        } else {
            this.isSuccess = false;     //采集失败
        }
        if (this.data.infoData == null) {
            this.data.infoData = {
                boxNo: '',
                outId: '',
                outNo: ''
            }
        }

    }

    toShowAlert() {
        this.showAlert = false;
    }

    btnCancel() {
        this.showAlert = true;
    }

    btnChoose() {
        this.showAlert = true;
        this.navCtrl.popToRoot(StoragePage);
    }

    goback() {
        this.navCtrl.popToRoot(StoragePage);
    }

    //删除并返回
    delAndBack() {
        console.log(this.service.boxNo);
        let param = {
            "boxNo": this.service.boxNo,        //箱码
            "outId": this.data.infoData.outId,        //出库单ID
            "outNo": this.data.infoData.outNo      //出库单号
        }
        this.storageServ.delscan(param)
            .then(res => {
                console.info("出库单_确认删除成功：", res);
                this.navCtrl.popToRoot(StoragePage);
            })
            .catch(err => {
                console.error("出库单_确认删除失败：", err);
                this.popup.alert(err.errorMassage);
            });
    }

    //删除并继续
    delAndContinue() {
        let param = {
            "boxNo": this.service.boxNo,            //箱码
            "outId": this.data.infoData.outId,      //出库单ID
            "outNo": this.data.infoData.outNo       //出库单号
        }
        this.storageServ.delscan(param)
            .then(res => {
                console.info("出库单_确认删除成功：", res);
                this.reDel();
            })
            .catch(err => {
                console.error("出库单_确认删除失败：", err);
                this.popup.alert(err.errorMassage);
            });

    }

    reDel() {
        this.barcodeScanner.scan().then((barcodeData) => {
            // Success! Barcode data is here
            console.log(barcodeData.text);
            if (barcodeData.text == null || barcodeData.text == '') {
                this.service.isReturn = true;
                return;
            }
            this.service.boxNo = barcodeData.text;
            let param = {
                "boxNo": barcodeData.text,        //箱码
                "outId": this.data.infoData.outId,        //出库单ID
                "outNo": this.data.infoData.outNo      //出库单号
            }
            this.storageServ.delscandetails(param)
                .then(res => {
                    console.info("出库单_确认删除成功：", res);
                    this.navCtrl.push(CjDelStatusPage, {data: res});
                })
                .catch(err => {
                    console.error("出库单_确认删除失败：", err);
                    this.navCtrl.push(CjDelStatusPage, {data: err});
                });
        }, (err) => {
            // An error occurred
            console.error(err);
        });

    }

}
